package top.malaoshi.webmvc.annotation.handler;

import top.malaoshi.webmvc.info.RequestMappingInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 处理RequestMapping、PostMapping、GetMapping等注解
 */
public class RequestMappingResolver {


    public RequestMappingInfo resolver(RequestMapping requestMapping) {
        String[] values = requestMapping.value();
        String[] paths = requestMapping.path();

        String[] pathsAll = resolverValueAndPath(values, paths);

        return RequestMappingInfo.of(pathsAll,requestMapping.method());
    }
    public RequestMappingInfo resolver(GetMapping getMapping) {
        String[] values = getMapping.value();
        String[] paths = getMapping.path();

        String[] pathsAll = resolverValueAndPath(values, paths);

        return RequestMappingInfo.of(pathsAll,getMapping.method());
    }
    public RequestMappingInfo resolver(PostMapping postMapping) {
        String[] values = postMapping.value();
        String[] paths = postMapping.path();

        String[] pathsAll = resolverValueAndPath(values, paths);

        return RequestMappingInfo.of(pathsAll,postMapping.method());
    }

    private String[] resolverValueAndPath(String[] value,String[] path){
        String[] values = null;
        int valuesLen = 0;
        if ( value!=null && value.length>0) {
            values = value;
            valuesLen = value.length;
        }
        String[] paths = null;
        int pathLen = 0;
        if (path!=null && path.length>0) {
            paths = path;
            pathLen = path.length;
        }
        String[] ret = new String[valuesLen + pathLen];
        if(valuesLen>0) {
            System.arraycopy(values, 0, ret, 0, values.length);
        }
        if(pathLen>0) {
            System.arraycopy(paths, 0, ret, values.length, paths.length);
        }

        return ret;
    }

}
